﻿using System;
using System.Data;
using BLL;
using Utils;
namespace UI.ModSalary
{
    //  REPLICATE( ' ',10-LEN(序号))+序号
    public partial class SalaryMonthEnd : System.Web.UI.Page
    {
        DefDicBiz defDicBiz = new DefDicBiz();
        SalaryTableBiz salaryTableBiz = new SalaryTableBiz();   
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                btnBankFormat.Attributes.Add("onclick", "javascript:ShowLink('DefBankFormat.aspx',800,600);");
                txtFileFormat.Text = salaryTableBiz.GetFileFormat();
                defDicBiz.InitXMLDropDownList(ddlYear, "YearList.xml", System.DateTime.Now.Year.ToString());
                defDicBiz.InitXMLDropDownList(ddlMonth, "MonthList.xml", (System.DateTime.Now.Month - 1).ToString());
                defDicBiz.InitDropDownList("Wage_Sets", ddlSetsID, "1");
                gvList.DataSource = salaryTableBiz.GetLists();
                gvList.DataBind();
            }
        }
        //结账
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            string setsID = ddlSetsID.SelectedValue;
            string msg = salaryTableBiz.ConfigMonthEnd(strYear, strMonth, setsID);
           JSUtility.Alert(this,msg);
        }
        //反
        protected void btnReAdd_Click(object sender, EventArgs e)
        {
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            string setsID = ddlSetsID.SelectedValue;
            string msg = salaryTableBiz.ConfigReMonthEnd(strYear, strMonth, setsID);
            JSUtility.Alert(this, msg);
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

        protected void btnBank_Click(object sender, EventArgs e)
        {
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            string setsID = ddlSetsID.SelectedValue;
            //获取文件格式
           string fileFormat= txtFileFormat.Text.Trim();
            string fileType=salaryTableBiz.GetFileType();
            bool flag=true;
            DataTable dt = salaryTableBiz.GetBankList(strYear, strMonth, setsID, fileFormat,fileType);
            if(fileType=="TXT")
            {
                
               flag= FileDirectoryUtility.CreatTxt(this,dt,"bankfile.txt");
            }
            else
            {
                ExcelUtility.ExportDsToXls(this, dt, "SalaryTable.xls");
            }
         //输出文本文件
           if(flag)
            JSUtility.Alert(this,"输出成功");
           else
               JSUtility.Alert(this, "输出失败");

        }

        protected void btnConfigFile_Click(object sender, EventArgs e)
        {
            string fileFormat = txtFileFormat.Text.Trim();
           
            if (salaryTableBiz.UpdateFileFormat(fileFormat))
            {
                JSUtility.Alert(this, "设置成功");
            }
            else
                JSUtility.Alert(this, "设置失败");
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            string setsID = ddlSetsID.SelectedValue;
            //if (StringUtility.GetDiffDays(strYear + "-" + strMonth + "-1", System.DateTime.Now.ToString("yyyy-MM-dd")) >= 2)
            //{
            //    JSUtility.Alert(this, "只能打印上个月的工资条！");
            //    return;
            //}
            DataTable dt = salaryTableBiz.GetLists(strYear, strMonth, setsID);
            ExcelUtility.ExportToXls(this, dt);
            //Response.Redirect("SalaryPrintSlip.aspx?SetsID=" + setsID + "&StrYear=" + strYear + "&StrMonth=" + strMonth);
        }
    }
}